# -*- coding: utf-8 -*-
"""
Created on Tue Jun  5 21:16:41 2018

@author: sun haiyang
"""
import math
from scipy import stats

#%%
class Coeff(object):
  #Coeff是一个父类。分别被 Pearson_coeff和Spearman_coeff继承
  def __init__(self,array1,array2):
    if len(array1) != len(array2):
      raise Exception("the elements of the two array should be equal")
    num = len(array1)

    #开始计算pearson或者spearman的coeff_pvalue
    print('array1:',array1)
    print('array2:',array2)
    self.coeff_pvalue = self.linear_corre(array1,array2)
    print('self.coeff_pvalue:',self.coeff_pvalue)
    try:
    #转换成z_score      
      self.coeff_z = self.z_score(self.coeff_pvalue[0])
      #将coeff_pavalue[0]中除以1/math.sqrt(num-3)
      self.coeff_z_std = self.divide_std(self.coeff_z,num)
    except:
      self.coeff_z = 'Error'
      self.coeff_z_std = 'Error'
      
  @staticmethod
  def linear_corre(array1,array2):
    pass
  
  @staticmethod
  def z_score(coeff):
    #将相关系数转换成z_score
    return 0.5 * math.log((1+coeff) / (1-coeff))
  
  @staticmethod
  def divide_std(coeff_z,num):
    #将z_score标准化
    return coeff_z * math.sqrt(num-3)

#%%
  
class Pearson_coeff(Coeff):
  #计算pearson correlation
  @staticmethod  
  def linear_corre(array1,array2):
    return list(stats.pearsonr(array1,array2))

class Spearman_coeff(Coeff):
  #计算pearson correlation
  @staticmethod
  def linear_corre(array1,array2):
    return list(stats.spearmanr(array1,array2))
